﻿using System;




namespace DailyFx.IndicatorExtension
{
   
	public class ATR : Index
	{

		private const Int32 cPeriod = 14;  // typically, 14 days.

		private SMA			movingAverage;
		private TR			trueRange;

		/// <summary>
		///		C'tor 
		/// </summary>
		public ATR ( int n ) : base (2, n)
		{
			this.movingAverage = new SMA ( cPeriod, n);
			this.trueRange = new TR (  n);

			this.MA = this.movingAverage.MAimpl;
			this.TR = this.trueRange.valimpl;
		//	this.TRimpl.Stylus = Stylus.HIDDEN;

		}


		public override void Cook( BarSeries series)
		{
			this.trueRange.Cook( series );
			this.movingAverage.Cook2 ( this.TR );
		}



		public override String Family
		{
			get { return "Average True Range"; }
		}

		public override String Label
		{
			get { return "ATR"; }
		}


		public override Int32 Rank
		{
			// Hides the TR .
			get { return 1;  }
		}

		public HyperVector MA  // moving average on TR . 
		{
			get { 
				return _children[ 0];
			}
			private set { 
				_children[ 0] = value;
			}
		}

		public HyperVector TR
		{
			get {
				return _children[1];
			}
			private set {
				_children[1] = value ;
			}
		}

	} // ks ATR


	public class TR : Index
	{
		/// <summary>
		///     c;tor
		/// </summary>
		public TR ( int n ) : base ( 1, n)
		{
			this.valimpl = new HyperVector ( n, "TR", Layout.SLAVE, Stylus.LINE, 0 );
		}


		public override void Cook ( BarSeries series )
		{
			decimal tr = 0.0m;

			for (int n = 0; n < series.Length; n++)   // forming
			{
				Bar prev = series[Math.Max(0, n - 1)];
				Bar curr = series[n];

				// get max movement 
				tr = Math2.Max	(	Math.Abs(curr.High - curr.Low	),
									Math.Abs(curr.High - prev.LastPx),
									Math.Abs(curr.Low - prev.LastPx	)
								);

				this.valimpl[ n] = tr;
			}
		}


		public override string Family
		{
			get { return "Average True Range"; }
		}

		public override string Label
		{
			get { return "TR"; }
		}




		public HyperVector valimpl
		{
			get {
				return _children[0];
			}
			private set {
				_children[ 0] = value ;	
			}
		}


	} // ks TR

} // ns DailyFx.IndicatorExtension